package BNS.kingPawn;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import BNS.graphs.*;

/** an extension of GraphPanel that draws edges and nodes, but DOES NOT DRAW tips. 
	(Naming due to previous intentions, which were revised.)
	<p>	TODO: at least fix this misnomer!
	<p>	TODO: consider eliminating this class in favour of ShowTipsPanel.
	*/
public class ShowNodeTipsPanel extends GraphPanel {
	
	static private final double wide=5.0;
	protected Polygon lustygon;
	protected EdgeState[] edgeStates;
	protected float[] tip;
	
	public ShowNodeTipsPanel(ColorMap cm) {
		super(cm);
		edgeStates= new EdgeState[0];
		lustygon= new Polygon();	// for highlighting edges
		for (int i=0; i<5; i++) lustygon.addPoint(0,0);
		System.err.println("constructed ShowNodeTipsPanel");
		}
	
	public void init(Graph gr) { super.init(gr); }
	
	public void setEdgeStates(EdgeState[] es) { edgeStates=es; }
	public void setNodeTips(float[] tip) { 
	  this.tip= tip; 
/*	  for (int i=0; i<tip.length; i++)
		System.err.format("  %5.2f",tip[i]);
	  System.err.println("   <== tips for "+ graph.eigenNode);
*/    }
	
	public synchronized void paint(Graphics g) {
		if (!ready(g)) return;
		drawEdges();
		drawMyNodes();
//		doEdgeStates();
//		offgraphics.drawString("NodeTips",50,50);
		commitImage(g);
		}
	
	/** Draws all nodes in the graph.
		<p>TODO: make this private? */
	public void drawMyNodes() {
//		System.err.print(" drawMyNodes");
		Enumeration eno= graph.enumNodes();
		while (eno.hasMoreElements()) {
			Node n= (Node)eno.nextElement();
			paintNode(offgraphics, n, render);
			//System.err.print(" "+n.color);
			}
		//System.err.println("  <== colors");
		}
	
	/** iterates through edgeStates, rendering each edge.
		<p>TODO: make this private? */
	protected void doEdgeStates() {
//		System.err.println("do my EdgeStates");
		offgraphics.setColor(Color.black);
		for (int i=0; i<edgeStates.length; i++) {
			drawTip(edgeStates[i].frIdx,edgeStates[i].toIdx,edgeStates[i].frTip);
			drawTip(edgeStates[i].toIdx,edgeStates[i].frIdx,edgeStates[i].toTip);
			}
		}
	
	private void drawTip(int frIdx, int toIdx, float tip) {
		Node frN= graph.getNode(frIdx);
		Point fr= frN.position();
		double x= fr.x-14;
		double y= fr.y+halfChar+5;
		String show= String.format("$%.2f", tip);
		offgraphics.drawString(show,(int)x,(int)y);
		}
	
	protected void paintNode(Graphics g, Node n, ColorMap render) {
//		System.err.println(" paintNode");
		Point npt= n.position();
		int x= npt.x;
		int y= npt.y;
		int w= 2*nodeRadius;
		int h= 2*nodeRadius;
		int clr= render.map(n.color);
		g.setColor(swatches[clr].color);
		g.fillOval(x-w/2, y-h/2,  w, h);
		g.setColor(Color.black);
		g.drawOval(x-w/2, y-h/2,  w, h);
		String tipStr= "  ";
		if (tip!=null) // might be null on first drawing
  		tipStr= String.format("%.2f",tip[n.getIndex()]);
		tipStr= "$"+tipStr.substring(1);
		if (n.color==MarketISGame.kingInt) tipStr= "KING";
		if (n.color==MarketISGame.pawnInt) tipStr= "pawn";
		if (n==graph.eigenNode) 
			g.drawString("you", x-10, y-halfChar);
		else 
			g.drawString("+"+n.otherEdges, x-10, y-halfChar);
		g.drawString(tipStr, x-14, y+2*halfChar);
		}
	
	}
